package com.gxa.admin.recycling.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.admin.recycling.service.GoodsService;
import com.gxa.admin.recycling.service.ImgService;
import com.gxa.core.recycling.common.JsonResult;
import com.gxa.core.recycling.pojo.Goods;
import com.gxa.core.recycling.pojo.Imgs;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 周志远
 */
@CrossOrigin
@RestController
@RequestMapping("/goods")
@Transactional(rollbackFor = Throwable.class)
@Api(tags = "商品管理页面")
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    ImgService imgService;
    @PostMapping("/getAll")
    @ApiOperation("查询所有+分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "商品id",required = false,dataType = "Long"),
            @ApiImplicitParam(name = "name",value = "商品名称",required = false,dataType = "String"),
            @ApiImplicitParam(name = "type",value = "商品类型",required = false,dataType = "String"),
            @ApiImplicitParam(name = "hot",value = "热兑商品",required = false,dataType = "Boolean"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "每页显示数",required = true,dataType = "Integer")

    })
    public Map findByPage(Long id,String name,String type,Boolean hot,Integer page,Integer size){
        System.out.println(hot);
        List<Map> allByPage = goodsService.findAllByPage(id, name, type, hot, page, size);
        List<Map> allByPage1 = goodsService.findAllByPage(id, name, type, hot, 1, 1000);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(allByPage);
        jsonResult.put("count",allByPage1.size());
        return jsonResult.getDataMap();
    }
    @PostMapping("/add")
    @ApiOperation("添加新的商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "商品名称",required = false,dataType = "String"),
            @ApiImplicitParam(name = "type",value = "商品类型",required = false,dataType = "String"),
            @ApiImplicitParam(name = "hot",value = "热兑商品",required = false,dataType = "Boolean"),
            @ApiImplicitParam(name = "needPoints",value = "兑换所需积分",required = false,dataType = "Integer"),
            @ApiImplicitParam(name = "counts",value = "库存",required = false,dataType = "Integer"),
            @ApiImplicitParam(name = "state",value = "上下架",required = false,dataType = "Boolean"),
            @ApiImplicitParam(name = "rank",value = "排序",required = false,dataType = "Integer"),
            @ApiImplicitParam(name = "remark",value = "详细描述",required = false,dataType = "String"),
            @ApiImplicitParam(name = "url",value = "图片地址（请测试多图时候，参数名能相同不）",required = false,dataType = "String")

    })
    public Map add(@RequestBody Goods goods){
        System.out.println(goods);
        Goods insert = goodsService.insert(goods);
        Goods byName = goodsService.findByName(goods.getName());
        Boolean state = byName.getState();
        if(state){
            goodsService.addGoodsToMongo(byName);
        }
        List<String> url = goods.getUrl();
        if (url.size()>0){
            for (String s : url) {
                Imgs imgs = new Imgs();
                imgs.setGid(byName.getId());
                imgs.setUrl(s);
                imgService.insert(imgs);
                Imgs byUrl = imgService.findByUrl(s);
                goodsService.addImgsToMongo(byUrl);
            }
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }
    @PostMapping("/byId")
    @ApiOperation("查询单个商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "商品id",required = false,dataType = "Long")

    })
    public Map findById(Long id){
        Goods goods = goodsService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(goods);
        return jsonResult.getDataMap();
    }
    @PostMapping("/update")
    @ApiOperation("更新商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "原来的商品id",required = false,dataType = "Long"),
            @ApiImplicitParam(name = "name",value = "修改后的商品名称",required = false,dataType = "String"),
            @ApiImplicitParam(name = "type",value = "修改后的商品类型",required = false,dataType = "String"),
            @ApiImplicitParam(name = "hot",value = "修改后的热兑商品",required = false,dataType = "Boolean"),
            @ApiImplicitParam(name = "needPoints",value = "修改后的兑换所需积分",required = false,dataType = "Integer"),
            @ApiImplicitParam(name = "counts",value = "修改后的库存",required = false,dataType = "Integer"),
            @ApiImplicitParam(name = "state",value = "修改后的上下架",required = false,dataType = "Boolean"),
            @ApiImplicitParam(name = "rank",value = "修改后的排序",required = false,dataType = "Integer"),
            @ApiImplicitParam(name = "remark",value = "修改后的详细描述",required = false,dataType = "String"),
    })
    public Map update(Goods goods){
        goodsService.updateMySqlAndMonge(goods);
        JsonResult jsonResult = new JsonResult();

            jsonResult.setCode("200");

        return jsonResult.getDataMap();
    }
}
